import {createStore} from "vuex";
import axios from "axios";
const store = createStore({
    state(){
        return{
            goods:[],
            sellerInfo:{},
            shopCarIsShow:false,
            shopCarList:[],
            sumPrice:0
        }
    },
    getters:{
       
    },
    mutations:{
      setSeller(state,payload){
        state.sellerInfo = payload.data;
      },
      setGoods(state,payload){
        state.goods = payload.data;
      },
      shopCarIsShow(state){
        state.shopCarIsShow = !state.shopCarIsShow;
      },
      setShopCarList(state,payload){
        const isHasItem = state.shopCarList.findIndex((item)=>{
          return item.name === payload.item.name
        });
        if(isHasItem === -1){
          state.shopCarList.push(payload.item);
        }
      },
      deleteShopCarList(state){
        const isHasItem = state.shopCarList.findIndex((item)=>{
          console.log(item.count);
          return item.count === 0;
        });
        if(isHasItem !== -1){
          state.shopCarList.splice(isHasItem,1);
        }
      },
      clearShopCar(state){
        state.shopCarList = [];
      }
    },
    actions:{
      async getSeller({commit}){
        const{
            data:{data},
        } = await axios.get("/api/seller");
        commit("setSeller",{
          data,
        })
      },
      async getGoods({commit}){
        const{
            data:{data},
        } = await axios.get("/api/goods");
        commit("setGoods",{
          data,
        })
      }
    }
});
export default store;